package com.tql.test_06;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;

/*
* BlockingQueue 阻塞队列
* 阻塞队列有四种阻塞处理机制 （队列存取数据采用的方法不同，就是不同的处理策略）
* 1.抛出异常（阻塞抛异常）
* 2.有返回值，不抛异常（有Boolean返回值 ）
* 3.阻塞等待（一直等，不抛异常）
* 4.超时等待（超时结束，不抛异常）
* */
public class BlockQueueTest {
    public static void main(String[] args) throws InterruptedException {
        test4();
    }
    //抛出异常 add() remove()
    public static void  test1(){
        //参数队列容量，是阻塞队列的大小
        ArrayBlockingQueue arrayBlockingQueue=new ArrayBlockingQueue(3);
        arrayBlockingQueue.add("a");
        arrayBlockingQueue.add("b");
        arrayBlockingQueue.add("c");
       /* //Exception in thread "main" java.lang.IllegalStateException: Queue full
        arrayBlockingQueue.add("d");*/
        System.out.println(arrayBlockingQueue.remove());
        System.out.println(arrayBlockingQueue.remove());
        System.out.println(arrayBlockingQueue.remove());
      /*  //Exception in thread "main" java.util.NoSuchElementException
        System.out.println(arrayBlockingQueue.remove());*/

    }
    //有返回值，不抛出异常 offer() poll()
    public static void  test2(){
        //参数队列容量，是阻塞队列的大小
        ArrayBlockingQueue arrayBlockingQueue=new ArrayBlockingQueue(3);
        System.out.println(arrayBlockingQueue.offer("a"));
        System.out.println(arrayBlockingQueue.offer("b"));
        System.out.println(arrayBlockingQueue.offer("c"));
        //队列已满 ，offer("d") 返回值为false
      /*  System.out.println(arrayBlockingQueue.offer("d"));*/
        System.out.println(arrayBlockingQueue.poll());
        System.out.println(arrayBlockingQueue.poll());
        System.out.println(arrayBlockingQueue.poll());
        //Exception in thread "main" java.util.NoSuchElementException
        //队列无数据，poll() 返回值为null
        System.out.println(arrayBlockingQueue.poll());
    }
    //阻塞等待（一直等待）put() take()
    public static void  test3() throws InterruptedException {
        //参数队列容量，是阻塞队列的大小
        ArrayBlockingQueue arrayBlockingQueue=new ArrayBlockingQueue(3);
        arrayBlockingQueue.put("a");
        arrayBlockingQueue.put("b");
        arrayBlockingQueue.put("c");
       /* //队列存放已满，会一直等待存放数据
        arrayBlockingQueue.put("d");*/
        System.out.println("==============================");
        arrayBlockingQueue.take();
        arrayBlockingQueue.take();
        arrayBlockingQueue.take();
        //队列以空，一直等待获取数据
        arrayBlockingQueue.take();

    }
    //超时等待，超过时间放弃存取 offer(,,) poll(,)
    public static void  test4() throws InterruptedException {
        //参数队列容量，是阻塞队列的大小
        ArrayBlockingQueue arrayBlockingQueue=new ArrayBlockingQueue(3);
        System.out.println(arrayBlockingQueue.offer("a"));
        System.out.println(arrayBlockingQueue.offer("b"));
        System.out.println(arrayBlockingQueue.offer("c"));
        System.out.println(arrayBlockingQueue.offer("d",2, TimeUnit.SECONDS));
        System.out.println(arrayBlockingQueue.poll());
        System.out.println(arrayBlockingQueue.poll());
        System.out.println(arrayBlockingQueue.poll());
        System.out.println(arrayBlockingQueue.poll(2,TimeUnit.SECONDS));
    }
}
